package com.karry.service.impl;

import com.karry.mapper.RecordMapper;
import com.karry.mapper.TypeMapper;
import com.karry.pojo.Type;
import com.karry.pojo.User;
import com.karry.service.TypeService;
import com.karry.util.Page;
import com.karry.util.SqlSessionFactoryUtils;
import com.karry.vo.ResultInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class TypeServiceImpl implements TypeService {

    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Type> searchAllType() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TypeMapper mapper = sqlSession.getMapper(TypeMapper.class);

        List<Type> types = mapper.searchAllType();
        sqlSession.close();
        return types;
    }

    @Override
    public List<Type> findTypeList(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TypeMapper mapper = sqlSession.getMapper(TypeMapper.class);

        List<Type> types = mapper.searchTypeListByUserID(id);
        sqlSession.close();
        return types;
    }

    @Override
    public ResultInfo<Type> deleteType(String typeId) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        TypeMapper mapper = sqlSession.getMapper(TypeMapper.class);

        int id = Integer.parseInt(typeId);

        int i = mapper.deleteType(id);

        ResultInfo<Type> resultInfo = new ResultInfo<>();
        if (i > 0){
            resultInfo.setCode(1);
        }else {
            resultInfo.setCode(0);
            resultInfo.setMsg("删除失败");
        }
        sqlSession.close();
        return resultInfo;

    }

    @Override
    public ResultInfo<Integer> addOrUpdate(String typeName, int id, String typeId) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        TypeMapper mapper = sqlSession.getMapper(TypeMapper.class);

        ResultInfo<Integer> resultInfo = new ResultInfo<>();
        Integer typeID = null;
        if (typeId !=null && typeId.length() > 0){
            typeID = Integer.parseInt(typeId);
        }

        if (typeName.length() > 14){
            resultInfo.setCode(0);
            resultInfo.setMsg("类型不能超过14位");
            sqlSession.close();
            return resultInfo;
        }

        //1.确定是否重名，参数：userId,typeName(和原类型也不能重名，否则修改无意义)
        int i = mapper.searchTypeNameExits(id, typeName);
        //2.不重名分析是否修改（typeId是否存在）
        if (i < 1){
            //获取用户类型数
            int count = mapper.getUserTypeCount(id);
            System.out.println("count:" + count);
            int row = 0;
            //分析是否修改
            Type type = null;
            if (typeID != null){
                type = mapper.searchTypeById(typeID);
            }

            if (type == null){
                //添加
                row = mapper.insertType(typeName, id, count + 1);
            }else {
                //修改
                row = mapper.updateType(typeName, typeID);
            }

            sqlSession.close();
            //3.添加或修改
            if (row > 0){
                resultInfo.setCode(1);
                resultInfo.setResult(count+1);
            }else {
                resultInfo.setCode(0);
                resultInfo.setMsg("更新失败");
            }
            return resultInfo;
        }else {
            //重名直接结束
            sqlSession.close();
            resultInfo.setCode(0);
            resultInfo.setMsg("重名");
            return resultInfo;
        }

    }

    @Override
    public Type searchTypeById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TypeMapper mapper = sqlSession.getMapper(TypeMapper.class);

        Type type = mapper.searchTypeById(id);
        sqlSession.close();
        return type;
    }

    @Override
    public Page<Type> getAllTypeByPage(int pageNum) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TypeMapper mapper = sqlSession.getMapper(TypeMapper.class);

        int count = mapper.getTypeCount();
        Page<Type> typePage = new Page<>(pageNum, 10, count);
        ArrayList<Type> types = mapper.getAllTypeListByPage((pageNum - 1) * 10);
        typePage.setDataList(types);
        System.out.println(typePage);
        sqlSession.close();
        return typePage;
    }

    @Override
    public ResultInfo<Integer> deleteType1(int id, HttpServletRequest request) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        TypeMapper mapper = sqlSession.getMapper(TypeMapper.class);
        RecordMapper recordMapper = sqlSession.getMapper(RecordMapper.class);
        ResultInfo<Integer> resultInfo = new ResultInfo<>();

        int i = mapper.deleteType(id);
        if (i > 0){
            resultInfo.setCode(1);
            User user = (User) request.getSession().getAttribute("user");
            Type type = mapper.getType(id);
            recordMapper.insertRecord("删除类型", type.getUserID(), -1 , "管理员删除该类型", "管理员ID: " + user.getId());

        }else {
            resultInfo.setCode(0);
            resultInfo.setMsg("删除失败");
        }
        sqlSession.close();
        return resultInfo;
    }

    @Override
    public ResultInfo<Integer> liveType(int id, HttpServletRequest request) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        TypeMapper mapper = sqlSession.getMapper(TypeMapper.class);
        RecordMapper recordMapper = sqlSession.getMapper(RecordMapper.class);
        ResultInfo<Integer> resultInfo = new ResultInfo<>();

        int i = mapper.liveType(id);
        if (i > 0){
            resultInfo.setCode(1);
            User user = (User) request.getSession().getAttribute("user");
            Type type = mapper.getType(id);
            recordMapper.insertRecord("恢复类型", type.getUserID(), -1 , "管理员恢复该类型", "管理员ID: " + user.getId());

        }else {
            resultInfo.setCode(0);
            resultInfo.setMsg("恢复失败");
        }
        sqlSession.close();
        return resultInfo;
    }
}
